package com.lyl.domaindemo.application.user;

import com.lyl.domaindemo.domain.user.User;
import com.lyl.domaindemo.domain.user.UserService;
import com.lyl.domaindemo.resource.user.dto.request.UserRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lyl
 * @date 2023/5/17
 */
@Component
public class UserUseCase {

    private final UserService userService;

    public UserUseCase(UserService userService) {
        this.userService = userService;
    }

    public Page<User> getUsers(Pageable pageable) {
        return userService.getUsers(pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveUser(UserRequest userRequest) {
        User user = User.builder().id(userRequest.getId())
                .username(userRequest.getUsername())
                .password(userRequest.getPassword())
                .sex(userRequest.getSex()).build();
        userService.saveUser(user);
    }

    public void deleteUser(List<Long> ids) {
        userService.deleteUser(ids);
    }
}
